package com.jsy.mini12306.manage.controller;

import com.fasterxml.jackson.databind.ObjectWriter;
import com.jsy.mini12306.common.util.JsonUtil;
import com.jsy.mini12306.manage.service.TrainPassStationService;
import com.jsy.mini12306.manage.service.TrainService;
import com.jsy.mini12306.manage.vo.TrainVO;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 车次（图中的路径）(Train)表控制层
 *
 * @author: SongyangJi
 * @since: 2021-09-03 02:58:41
 * @description:
 */

@Slf4j
@RestController
@RequestMapping("train")
public class TrainController {
    /**
     * 服务对象
     */
    @Resource
    private TrainService trainService;

    @Resource
    TrainPassStationService trainPassStationService;

    @PostMapping("/new")
    public ResponseEntity<Boolean> postNewTrain(@RequestBody TrainVO trainVO) {
        System.out.println(trainVO);
        return ResponseEntity.ok(true);
    }

    @PostMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Boolean> create(@RequestPart("train") TrainVO trainVO ,@RequestPart("file") MultipartFile file) throws IOException {
//        System.out.println(trainVO);
        InputStream inputStream = file.getInputStream();
//        trainPassStationService.saveBatchTrainPassStationsFromExcel(1,inputStream);
        trainPassStationService.saveNewTrain(trainVO,inputStream);
        return ResponseEntity.ok(true);
    }


}
